// pages/order/orderedStatus/orderedStatus.js
const handler = require("../../../utils/function.js");
var app = getApp(),
    api = require("../../../api.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
      img: handler.driverImg,
        driver: {},
        order: {},
        markers: [],
        polyline: [{
            points: [{
                longitude: wx.getStorageSync("startAddress").longitude,
                latitude: wx.getStorageSync("startAddress").latitude
            }, {
                longitude: 108.94,
                latitude: wx.getStorageSync("startAddress").latitude
            }],
            color: "#0299de",
            width: 9,
            dottedLine: false,
            arrowLine: true,
        }],
        controls: [{
            id: 1,
            iconPath: '../../../images/icon/icon-22.png',
            position: {
                left: 10,
                top: 10,
                width: 50,
                height: 50
            },
            clickable: true
        }, {
            id: 2,
            iconPath: '../../../images/icon/icon-30.png',
            position: {
                top: 280,
                left: 10,
                width: 50,
                height: 50
            },
            clickable: true,
            order_no: ''
        }, ],
        longitude: null,
        latitude: null,
        ordernum:0,//累计使用代驾次数
    },
    // 获取位置路线
    // getPolyline: function() {
    //     handler.getPolyline((res) => {
    //         console.log(res);
    //         const polyline = this.data.polyline;
    //         polyline[0].points = res;
    //         this.setData({
    //             polyline
    //         });
    //     })
    // },
    // intoPageHandler 页面跳转
    intoPageHandler: function(e) {
        var that = this;
        // console.log(e.currentTarget.dataset.skip);
        app.request({
            url: api.order.ordering,
            method: 'post',
            data: {
                order_no: e.currentTarget.dataset.order_no,
            },
            success: function(rs) {
                if (rs.code == 1) {
                    //wx.setStorageSync("current_order", e.currentTarget.dataset.order_no)
                    handler.intoPageHandler(e.currentTarget.dataset.skip)
                } else {
                    wx.showToast({
                        title: rs.msg,
                        duration: 1000
                    })
                }
            }
        })
    },
    // callPhoneHandler 打电话
    callPhoneHandler: function(e) {
        console.log(e.currentTarget.dataset.phone)
      handler.callPhoneHandler(e.currentTarget.dataset.phone);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            order_no: options.order_no,
        })
        
    },
  getPolyline: function (slocaltion) {
    const that=this;
    const { polyline}=this.data;
    const froms = slocaltion[1].latitude + "," + slocaltion[1].longitude + "&to=" + slocaltion[0].latitude + "," + slocaltion[0].longitude;
    wx.request({
      url: `https://apis.map.qq.com/ws/direction/v1/driving/?from=${froms}&key=VD2BZ-SUEK4-Y5OU7-XPL6G-K4AB5-BWBJN`,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data.status === 384) { } else {
          var coors = res.data.result.routes[0].polyline;
          for (var i = 2; i < coors.length; i++) {
            coors[i] = coors[i - 2] + coors[i] / 1000000
          }
          // console.log(coors);
          const points = [];
          for (var j = 0; j < coors.length; j += 2) {
            points[points.length] = {
              longitude: coors[j + 1],
              latitude: coors[j]
            }
          }
          polyline[0].points = points;
          that.setData({ polyline: polyline})
        }
      },
      fail: function () {
        // showToast('网络出现错误', "none")
      }
    })
  },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        console.log('抢单结果22', that.data.order_no)
        wx.showLoading({
          title: '',
        })
        app.request({
            url: api.order.getfind,
            method: 'post',
            data: {
                order_no: that.data.order_no,
            },
            success: function(rs) {
              wx.hideLoading()
                that.setData({
                    order: rs.data,
                    markers: rs.data.slocaltion,
                    driver: wx.getStorageSync("driver"),
                    longitude: rs.data.elocaltion.lng,
                    latitude: rs.data.elocaltion.lat,
                    ordernum:rs.data.ordernum
                })
              that.getPolyline(rs.data.slocaltion);
                wx.setStorageSync("server_phone", rs.data.passenger_phone);
                wx.setStorageSync("ordernum", rs.data.ordernum);
            }
        })
        this.setData({
            //order: wx.getStorageSync("current_order"),
        });
    },
  
})